var PurchaseAndOrdesdetail = new Vue({
    el: '#PurchaseAndOrdesdetail',
    //初始化数据类型
    data: {
        //自主添加商品信息
        addYourselfSwitch:true,
        isEnter: '',
        formLabelWidth: '140px',
        dialogFormVisible: false,
        dialogFormDtils: false,
        dialogFormContrast:false,
        //页面数据
        urchaseAndOrdersdetail: {
            id: '',
            company_name: '', // 采购单位
            lxr: '', // 联系人
            phone: '', // 联系电话
            entry_name: '', // 项目名称
            bhdget_amount: '', //  预算金额
            category: '', //   类别
            bankroll_source: '', // 资金来源
            czbksr: '', // 财政拨款收入
            ysxzsysf: '', //  预算管理的行政事业收费
            yszfxjj: '', // 预算管理的政府性基金
            ysqtzffssr: '', //  预算管理的其他政府非税收入
            czzhggfsssr: '', // 财政专户管理的政府非税收入
            sjczzyzf: '', // 上级财政转移支付
            sysr: '', //   事业收入
            yysr: '', // 经营收入
            sjbzsr: '', // 上级补助收入
            fsdwsjsr: '', //  附属单位上缴收入
            qtsr: '', // 其他收入
            cgdwyj: '', //  采购单位意见
            sqdwfzr: '', // 申请单位负责人签名
            sqdwqmsj: '', // 申请单位负责人签名时间
            zgbmfzr: '', //  主管部门负责人签名
            zgbmfzrqmsj: '', // 主管部门负责人签名时间
            bmyskssqrqm: '', //   部门预算管理科室申请人签名
            bmysksfzrqm: '', //  部门预算管理科室负责人签名
            bmysksfzrqmsj: '', //  部门预算管理科室签名时间
            szfcgbgsyj: '', // 市政府采购办公室意见
            szfcgbgsjbr: '', // 市政府采购办公室经办人签名
            szfcgbsjfzr: '', // 市政府采购办公室负责人签名
            szfcgbsjfzrqmsj: '', //   市政府采购办公室签名时间
            cgxs: '',//采购形式
            wtdljg: '',//委托代理机构
            zfcgfs: '',//政府采购方式
            type: '',//订单状态
            creator_userid: '',
            createtime: '',
            total: '',//合计金额
            shippingAddress: '',
            //在审核阶段关闭金额来源等不可改动
            ifInput:'',
            //查询所使用开关
            ifQuery:false,
            //作废开关
            ifClose:false,
            //申请作废开关
            purchaseClose:false,
            //全局查询使用，如果是true则全部按钮均不出现
            queryFlag:false
        },
        ordersDetails: [],
        ok: '',
        form: {
            goodsType: [],
            //商品类型，排序
            category: [],
            selectedOptions: [],
            //页面数据
            purchaseOrderDetail: {
                goodsName: '',
                //品目名称
                pmmc: '',
                //品目编码
                pmbm: '',
                //规格型号
                ggxh: '',
                //主要配置及技术参数
                jscs: '',
                //单位
                dw: '',
                //数量
                sl: '',
                //单价
                dj: '',
                //总价
                zj: '',
                goodsType: ''
            },
            purchaseType: '',
            purchaseOrders: '',
        },
        //商品对比数据
        contrastInformation:[],
    },
    //时间提交方法
    methods: {
        deleteContrastDetail:function (shopData) {
            var _this=this;
            $.ajaxSetup({
                async: false
            });
            $.post("/home/purchase_orders/deleteDetailById", {"data": JSON.stringify(shopData),"order": JSON.stringify(_this.urchaseAndOrdersdetail)}, function (data) {
                if(data.code==200){
                    debugger
                    _this.urchaseAndOrdersdetail.bhdget_amount  = data.data.bhdget_amount;
                    _this.$notify({
                        message: "删除成功",
                        type: 'success'
                    });
                }else{
                    _this.$notify({
                        message: "删除失败",
                        type: 'error'
                    });
                }
            });
            //再次更新列表
            $.post("/home/purchase_orders/selectPurchaseDetailedListAllByOrdersId",
                {"ordersId": _this.urchaseAndOrdersdetail.id},
                function (data) {
                    _this.ordersDetails = data.data.purchaseDetailedList;
                });
        },
        queryContrastDetail:function (shopData) {
            var _this=this;
            debugger
            _this.contrastInformation=null;
            //根据采购清单去查询对比数据
            $.post("/home/purchase_orders/selectContrastDetail", {"data": JSON.stringify(shopData)}, function (data) {
                _this.contrastInformation=data.data.commodity;
            });
            _this.dialogFormContrast=true;
        },

        selectKeyNotice: function (id) {
            var _this = this;
            $.post("/home/purchase_orders/selectOrdersById", {"ordersId": id}, function (data) {
                _this.urchaseAndOrdersdetail.type = data.data.purchaseOrders.type;
                _this.urchaseAndOrdersdetail.creator_userid = data.data.purchaseOrders.creator_userid;
                _this.urchaseAndOrdersdetail.createtime = data.data.purchaseOrders.createtime;
            });
        },

        //计算总价
        onChanges: function () {
            debugger
            var _this = this;
            _this.urchaseAndOrdersdetail.total = Number(_this.urchaseAndOrdersdetail.czbksr) + Number(_this.urchaseAndOrdersdetail.ysxzsysf) + Number(_this.urchaseAndOrdersdetail.yszfxjj) + Number(_this.urchaseAndOrdersdetail.ysqtzffssr) + Number(_this.urchaseAndOrdersdetail.czzhggfsssr) + Number(_this.urchaseAndOrdersdetail.sjczzyzf) + Number(_this.urchaseAndOrdersdetail.sysr) + Number(_this.urchaseAndOrdersdetail.yysr) + Number(_this.urchaseAndOrdersdetail.sjbzsr) + Number(_this.urchaseAndOrdersdetail.fsdwsjsr) + Number(_this.urchaseAndOrdersdetail.qtsr);
            if(Number(_this.urchaseAndOrdersdetail.total)<Number(_this.urchaseAndOrdersdetail.bhdget_amount)){
                _this.$notify({
                    message: "对不起,您的年初预算金额不足！",
                    type: 'error'
                });
            }
        },
        toSave: function (entity, id) {
            var _this = this;
            _this.onChange();
            _this.onChanges();
            if (_this.noPass(_this.urchaseAndOrdersdetail.total, _this.ordersDetails)) {
                _this.$notify({
                    message: "请注意，采购清单总额与资金来源总额不符！保存失败。",
                    type: 'error'
                });
            } else {
            _this.selectKeyNotice(id);
            $.post("/home/purchase_orders/updatePurchaseOrdersById", {"data": JSON.stringify(_this.urchaseAndOrdersdetail)}, function (data) {
                //更新主单
                _this.status = data.data.status;
                if (_this.status == "true") {
                    _this.$notify({
                        message: "保存成功",
                        type: 'success'
                    });
                }
            });
        }
        },
        toAdd: function (entity, id) {

            var _this = this;
            $.post("/home/menu/select", "", function (data) {
                _this.form.goodsType = data.data.Menu;
                //循环第一级菜单
                for (var i = 0; i < data.data.Menu.length; i++) {
                    var map1 = {};
                    map1.children = [];
                    map1.value = data.data.Menu[i].id;
                    map1.label = data.data.Menu[i].name;
                    //循环第二级菜单
                    for (var j = 0; j < data.data.Menu[i].childMenus.length; j++) {
                        var map2 = {};
                        map2.value = data.data.Menu[i].childMenus[j].id;
                        map2.label = data.data.Menu[i].childMenus[j].name;
                        map2.children = [];
                        //循环第三菜单
                        for (var z = 0; z < data.data.Menu[i].childMenus[j].childMenus.length; z++) {
                            var map3 = {};
                            map3.value = data.data.Menu[i].childMenus[j].childMenus[z].id;
                            map3.label = data.data.Menu[i].childMenus[j].childMenus[z].name;
                            map3.type = data.data.Menu[i].childMenus[j].childMenus[z].coding;
                            map2.children.push(map3);
                        }
                        map1.children.push(map2);

                    }
                    _this.form.category.push(map1);
                }

            });

            _this.dialogFormVisible = true;
        },
        toBinbind: function (esdfsdfn, flag) {
            var isJump=false;
            debugger
            $.ajaxSetup({
                async: false
            });
            var _this = this;
            //查询登录用户
            $.post("/sys/user/userSelect", "", function (data) {
                _this.User = data.data.sysUser;
            });
            //查看这个清单是否已经询价，已经询价之后就不能再次询价

            if(flag) {
                var address = '';
                $.post("/home/quote_or_compare/selectByUserId_", {"id": esdfsdfn.id}, function (data) {
                    if (data.code == 200) {
                        if(data.data.addressManagers.length>0){
                            address = data.data.addressManagers[0].address;
                        }

                        debugger
                        if (data.data.size == '0') {
                            isJump = true;
                        } else {
                            _this.$notify({
                                message: "该清单正在询价，不允许再次询价，或者关闭正在询价的清单然后再此询价！",
                                type: 'error'
                            });
                            isJump = false;
                        }
                    }
                });
                if(isJump){
                    $("#wrap-left").load("/home/bidding4purchaseOrdes.html", function () {
                        bidding4purchase.QuoteOrCompare.commodityName = esdfsdfn.pmmc;
                        bidding4purchase.QuoteOrCompare.commodityNum = esdfsdfn.sl;
                        bidding4purchase.QuoteOrCompare.biddingName = _this.urchaseAndOrdersdetail.company_name
                        bidding4purchase.urchaseAndOrdersdetails = _this.urchaseAndOrdersdetail;
                        bidding4purchase.ordersDetails = _this.ordersDetails;
                        bidding4purchase.QuoteOrCompare.purchaseId = esdfsdfn.id;
                        bidding4purchase.pmbm = esdfsdfn.pmbm;
                        bidding4purchase.goodsType = esdfsdfn.goodsType;
                        bidding4purchase.QuoteOrCompare.shippingAddress = address;
                    });
                    _this.dialogFormVisible = false;
                }
            }else{
                debugger
                //关闭历史询价
                $.post("/home/quote_or_compare/uodateTypeAll", {"id":esdfsdfn.id}, function (data) {
                    if(data.code==200){
                        _this.$notify({
                            message: "关闭历史询价成功！",
                            type: 'success'
                        });
                    }else{
                        _this.$notify({
                            message: "关闭历史询价失败！",
                            type: 'error'
                        });
                    }
                });
            }
        },
        toShoping: function (shopData, flags) {
            var _this=this;
            $.ajaxSetup({
                async: false
            });
            //购买之前先关闭之前可能性存在的 询价单
            $.post("/home/quote_or_compare/uodateTypeAll", {"id":shopData.id}, function (data) {
                if(data.code==200){
                    _this.$notify({
                        message: "直接购买之前关闭历史询价成功！",
                        type: 'success'
                    });
                }else{
                    _this.$notify({
                        message: "直接购买之前关闭历史询价失败！",
                        type: 'error'
                    });
                }
            });
            $.post("/home/purchase_orders/updateDetailById4type", {
                    "data": JSON.stringify(shopData),
                    "type": "结果确认"
                }, function (data) {
                    //更新子单状态为结果确认
                    if (data.code == 200) {
                        //刷新列表
                        $.post("/home/purchase_orders/selectPurchaseDetailedListAllByOrdersId",
                            {"ordersId": _this.urchaseAndOrdersdetail.id},
                            function (data) {
                                //需要重新加载采购清单管理
                                _this.ordersDetails = data.data.purchaseDetailedList;
                                $("#wrap-left").load("http://" + window.location.host + "/home/personOffers.html", function () {

                                });


                            });
                        _this.$notify({
                            message: "购买成功",
                            type: 'success'
                        });
                    }else {
                        _this.$notify({
                            message: data.data.message,
                            type: 'error'
                        });
                        return;
                    }
                }
            );
        },
        openAddPurchaseDetils: function (shopData, type) {
            var _this = this;
            $("#wrap-left").load("http://" + window.location.host + "/home/addPurchaseOrdesdetail.html", function () {
                addPurchaseOrderDetail.purchaseOrderDetail = shopData;
                addPurchaseOrderDetail.purchaseType = type;
            });
        },
        toPass: function (type) {
            debugger
            var _this = this;
            $.ajaxSetup({
                async: false
            });
            if( _this.noPass(_this.urchaseAndOrdersdetail.total,_this.ordersDetails)){
                _this.$notify({
                    message: "请注意，采购清单总额与资金来源总额不符！",
                    type: 'error'
                });
                return;
            };
            if (type == "提交审核") {
                if(_this.urchaseAndOrdersdetail.entry_name ==null||_this.urchaseAndOrdersdetail.bhdget_amount=='0.0'){
                    _this.$notify({
                        message: "请注意，项目名称必须填写，并且采购预算不可以为0！",
                        type: 'error'
                    });
                    return;
                }
                $.post("/home/purchase_orders/toPassDetailByParentId", {
                    "parentData": JSON.stringify(_this.urchaseAndOrdersdetail),
                    "data": _this.ordersDetails
                }, function (data) {
                    //更新主单
                    if (data.code == 200) {
                        _this.status = data.data.status;
                        if (_this.status == "true") {
                            _this.$notify({
                                message: "提交审核成功",
                                type: 'success'
                            });
                            //重新加载待审核列表
                            $("#wrap-left").load("http://" + window.location.host + "/home/PurchaseDetailListQuery.html", function () {

                            });
                        }
                    } else {
                        _this.$notify({
                            message: data.data.message,
                            type: 'error'
                        });
                        return;
                    }
                });
            }else{
                $.post("/home/purchase_orders/toCenterDetailByParentId", {
                    "parentData": JSON.stringify(_this.urchaseAndOrdersdetail),
                    "data": _this.ordersDetails,"type":type
                }, function (data) {
                    //更新主单
                    if (data.code == 200) {
                        _this.status = data.data.status;
                        if (_this.status == "true") {
                            _this.$notify({
                                message: "操作成功",
                                type: 'success'
                            });
                            $("#wrap-left").load("http://" + window.location.host + "/home/PurchaseDetailListEnterByParent.html", function () {

                            });
                        }
                    } else {
                        _this.$notify({
                            message: data.data.message,
                            type: 'error'
                        });
                        return;
                    }
                });
            }
        },
        noPass:function (total,ordersDetails) {
            debugger
            var flag=false;
            var sumTotal=0;
            if(total==''||total==null||total=='undefined'){
                _this.$notify({
                    message: "年初预算合计总额为空，请检查！",
                    type: 'error'
                });
                return true;
            }
            if(ordersDetails.length<0){
                _this.$notify({
                    message: "没有采购清单不允许提交审核，请检查！",
                    type: 'error'
                });
                return true;
            }

        },
        checkRole: function (str) {
            var _this = this;
            var RegPrice = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
            if (!RegPrice.test(str)) {
                _this.$notify({
                    message: "请正确填写,必须为数字.",
                    type: 'error'
                });
            }
        },
        //添加商品信息
        addPurchaseDetail: function () {
            var _this = this;

            //市场价格/供货价格正则
            var RegPrice = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
            ////库存正则
            var Regstock = /^[0-9]{1,5}$/;
            var name1 = _this.form.selectedOptions;
            var name2 = _this.form.goodsName;
            var name3 = _this.form.ggxh;
            var name4 = _this.form.dw;
            var name5 = _this.form.sl;
            var name6 = _this.form.dj;
            var name7 = _this.form.jscs;
            var name8 = _this.form.pmbm;
            if ((name1 == "") || (name2 == "") || (name3 == "") || (name4 == "") || (name5 == "") || (name6 == ""|| (name6 == "")) || (name7 == "")|| (name8 == "")) {
                _this.$notify({
                    message: "对不起，关键参数不允许为空.必须将所有内容规范填写！",
                    type: 'error'
                });
                return;
            } else {
                $.ajaxSetup({
                    async: false
                });
                //判断这个清单是否已经存在
                $.post("/home/purchase_orders/selectDetailById", {"data": JSON.stringify(_this.form.purchaseOrderDetail)}, function (data) {
                    _this.ttype = data.data.status;
                });
                if (_this.ttype == "true") {
                    if (_this.ordersDetails.length == 0) {
                        //如果没有朱采购单实体就认为是第一次
                        $.post("/home/purchase_orders/insertDetailAndAddOrders4Frist", {"data": JSON.stringify(_this.form.purchaseOrderDetail)}, function (data) {
                            if (data.code == 200) {
                                _this.flag = data.data.status;
                                _this.f_id = data.data.f_id;
                                _this.purchaseOrders = data.data.purchaseOrders;
                            } else {
                                _this.$notify({
                                    message: data.data.message,
                                    type: 'error'
                                });
                                return;
                            }
                        });
                    } else {
                        $.post("/home/purchase_orders/addDetailByParentId", {
                            "data": JSON.stringify(_this.form.purchaseOrderDetail),
                            "parentData": JSON.stringify(_this.urchaseAndOrdersdetail)
                        }, function (data) {
                            if (data.code == 200) {
                                debugger
                                _this.urchaseAndOrdersdetail.bhdget_amount = data.data.purchaseOrders.bhdget_amount;

                                $.post("/home/purchase_orders/selectPurchaseDetailedListAllByOrdersId",
                                    {"ordersId": data.data.parentId},
                                    function (data) {
                                        _this.ordersDetails = data.data.purchaseDetailedList;
                                    });
                            } else {
                                _this.$notify({
                                    message: data.data.message,
                                    type: 'error'
                                });
                                return;
                            }
                        });
                    }


                } else {
                    $.ajaxSetup({
                        async: false
                    });
                    $.post("/home/purchase_orders/updateDetailById", {"data": JSON.stringify(_this.form.purchaseOrderDetail)},
                        function (data) {
                            if (data.code == 200) {
                                _this.flag = data.data.status;
                                _this.f_id = data.data.f_id;
                                if (_this.flag === "true") {
                                    _this.$notify({
                                        message: "清单更新成功.",
                                        type: 'success'
                                    });

                                }
                            } else {
                                _this.$notify({
                                    message: data.data.message,
                                    type: 'error'
                                });
                                return;
                            }
                        });
                }
            }
            _this.dialogFormVisible = false;
        },
        //计算总价
        onChange: function () {
            var _this = this;
            _this.form.purchaseOrderDetail.zj = Number(_this.form.purchaseOrderDetail.dj) * Number(_this.form.purchaseOrderDetail.sl);
        },
        onSelectMenu: function () {
            debugger
            var _this = this;
            for (var i = 0; i < _this.form.goodsType.length; i++) {
                if (_this.form.selectedOptions[0] == _this.form.goodsType[i].id) {
                    _this.form.purchaseOrderDetail.goodsType = _this.form.selectedOptions[0];
                    for (var j = 0; j < _this.form.goodsType[i].childMenus.length; j++) {
                        if (_this.form.selectedOptions[1] == _this.form.goodsType[i].childMenus[j].id) {

                            for (var z = 0; z < _this.form.goodsType[i].childMenus[j].childMenus.length; z++) {
                                if (_this.form.selectedOptions[2] == _this.form.goodsType[i].childMenus[j].childMenus[z].id) {

                                    _this.form.purchaseOrderDetail.pmbm = _this.form.goodsType[i].childMenus[j].childMenus[z].coding;
                                    _this.form.purchaseOrderDetail.pmmc = _this.form.goodsType[i].childMenus[j].childMenus[z].name;
                                }
                            }
                        }
                    }

                }
            }
        },
    },
    //页面加载完成后执行此方法
    created: function () {
        var _this=this;
        $.post("/home/purchase_orders/configuration", {}, function (data) {
            if(data.data.addYourselfSwitch == "Y"){
                _this.addYourselfSwitch = true;
            }else{
                _this.addYourselfSwitch = false;
            }

        });

//查询主单
        if (_this.urchaseAndOrdersdetail.id!=null&&_this.urchaseAndOrdersdetail.id!='') {
            debugger
            $.post("/home/purchase_orders/selectOrdersById", {"ordersId": _this.urchaseAndOrdersdetail.id}, function (data) {
                if(data.data.purchaseOrders !=null){
                    _this.urchaseAndOrdersdetail=data.data.purchaseOrders;
                }
            });
        }
        if(_this.urchaseAndOrdersdetail.type == '待审核'||_this.urchaseAndOrdersdetail.type == '审核通过'){
            _this.urchaseAndOrdersdetail.ifInput=true;
        }
    },

});